package com.ideal.manage.esms.compoment;

import com.ideal.manage.esms.service.report.ReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;

/**
 * @author Allen
 * 系统报告定时任务
 */
@Component
public class ReportTask {
    private Logger logger = LoggerFactory.getLogger(ReportTask.class);
    @Autowired
    private ReportService reportService;
    @Value("${config.task.reportTask.enable}")
    private Boolean reportTaskEnable;
    @Value("${config.task.reportYearTask.enable}")
    private Boolean reportYearTaskEnable;

    /**
     * 每月1号零点执行
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    public void reportTask() {
        String name = null;
        if (reportTaskEnable) {
            try {
                reportService.saveReportMonth(name);
                logger.info("月报生成成功!");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            logger.debug("reportTask任务展厅");
        }
    }

    /**
     * 每年的最后一月的23点的59分59秒开始任务
     */
    @Scheduled(cron = "59 59 23 31 12 ?")
    public void reportYearTask() {
        String name = null;
        if (reportYearTaskEnable) {
            try {
                reportService.saveReportYear(name);
                logger.info("年报生成成功!");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            logger.debug("reportYearTask任务展厅");
        }
    }
}
